<template>
  <el-aside :width="width">
    <el-menu
       background-color="#545c64"
       text-color="#fff"
       :collapse="isCollapse"
       :collapse-transition="false"
       :default-active="activeMenu"
      >
      <h1 v-show="!isCollapse">怡合达后台管理系统</h1>
      <h1 v-show="isCollapse">后台管理系统</h1>
      <el-menu-item 
      v-for="item in noChildren"
      :index="item.path"
      :key="item.path"
      @click="handleMenu(item)"
      >
          <component class="icons" :is="item.icon"></component>>
          <span>{{item.label}}</span>
      </el-menu-item>
        <el-sub-menu 
        v-for="item in hasChildren"
       :index="item.path"
       :key="item.path"
      
        >
          <template #title>
            <component class="icons" :is="item.icon"></component>>
            <span>{{item.label}}</span>
          </template>
          <el-menu-item-group >
            <el-menu-item 
            v-for="(SubItem,SubIndex) in item.children"
           :index="SubItem.path"
           :key="SubItem.path"
           @click="handleMenu(SubItem)"
            >
            <component class="icons" :is="SubItem.icon"></component>>
            <span>{{SubItem.label}}</span>
           </el-menu-item>
           
          </el-menu-item-group>
        
        </el-sub-menu>
     
       
      </el-menu>

  </el-aside>
</template>

<script setup>
import { useAllDataStore } from "../stores/index";
import {ref,computed} from 'vue'
import { useRoute, useRouter } from 'vue-router';

const store=useAllDataStore()
const isCollapse=computed(()=>store.state.isCollapse)
const width=computed(()=>store.state.isCollapse?"64px":"180px")
const list =ref([
      	{
          path: '/home',
          name: 'home',
          label: '首页',
          icon: 'house',
          url: 'Home'
      	},
        {
            path: '/mall',
            name: 'mall',
            label: '商品管理',
            icon: 'video-play',
            url: 'Mall'
        },
        {
            path: '/user',
            name: 'user',
            label: '用户管理',
            icon: 'user',
            url: 'User'
        },
        {
            path: 'other',
            label: '其他',
            icon: 'location',
            children: [
                {
                    path: '/page1',
                    name: 'page1',
                    label: '页面1',
                    icon: 'setting',
                    url: 'Page1'
                },
                {
                    path: '/page2',
                    name: 'page2',
                    label: '页面2',
                    icon: 'setting',
                    url: 'Page2'
                }
            ]
        }
])
const noChildren = computed(() => list.value.filter(item => !item.children))
const hasChildren =computed(() => list.value.filter(item => item.children))
const route=useRoute()
const router=useRouter()
const activeMenu=computed(()=>route.path)
const handleMenu=(item)=>{
    router.push(item.path)
    store.selectMenu(item)
}
</script>

<style scoped lang="less">
.icons{
  width:20px;
  height: 20px;
  margin-right:5px;

}
.el-menu{
  border-right: none;
  h1{
    line-height: 48px;
    color:#fff;
    text-align: center;
  }
}

.el-aside{
  height: 100%;
  background-color: #545c64;
}
</style>